package top.lyjwn.ajui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;


import androidx.annotation.NonNull;
import top.lyjwn.ajui.R;

public class CenterDialog  extends Dialog {

    private Context context;
    private int layoutResId;

    public CenterDialog(@NonNull Context context, int layoutResId, CenterDialog.Build build) {
        super(context, R.style.bottom_dialog_theme);
        this.context = context;
        this.layoutResId = layoutResId;
        View view = View.inflate(context, layoutResId, null);
        setContentView(view);
        setBuild(build);
    }
    public CenterDialog(@NonNull Context context, int layoutResId) {
        super(context, R.style.bottom_dialog_theme);
        this.layoutResId = layoutResId;
        this.context = context;

    }

    public void setBuild(CenterDialog.Build build){
        View view = View.inflate(context, layoutResId, null);
        setContentView(view);
        Window window = getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
        window.setWindowAnimations(R.style.yes_or_no_dialog_anim_style);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View cancel = view.findViewById(R.id.cancel);
        if (cancel != null) {
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        build.init(view,this);
    }


    public interface Build {
        void init(View v, CenterDialog bd);
    }
}

